﻿namespace HuaWei
{
    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Security.Cryptography;
    using System.Text;

    public static class EncryptDecrypt
    {
        private const string str = "GHjiEeWw";

        public static string Decrypt(string pToDecrypt, EncryptionType type)
        {
            if (type != EncryptionType.DES)
            {
                return pToDecrypt;
            }
            return smethod2(pToDecrypt, "GHjiEeWw");
        }

        public static void DecryptFile(string srcFile, string dstFile)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            provider.Key = Encoding.ASCII.GetBytes("GHjiEeWw");
            provider.IV = Encoding.ASCII.GetBytes("GHjiEeWw");
            FileStream stream = new FileStream(srcFile, FileMode.Open, FileAccess.Read);
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Read);
            StreamReader reader = new StreamReader(stream2, new ASCIIEncoding());
            string s = reader.ReadToEnd();
            reader.Close();
            stream2.Close();
            byte[] bytes = Encoding.Default.GetBytes(s);
            try
            {
                stream = new FileStream(dstFile, FileMode.Create, FileAccess.ReadWrite);
                stream.Write(bytes, 0, bytes.Length);
                stream.Flush();
            }
            finally
            {
                stream.Close();
            }
        }

        public static string Encrypt(string pToEncrypt, EncryptionType type)
        {
            if (type != EncryptionType.DES)
            {
                return pToEncrypt;
            }
            return smethod1(pToEncrypt, "GHjiEeWw");
        }

        public static void EncryptFile(string srcFile, string dstFile)
        {
            FileInfo info = new FileInfo(srcFile);
            byte[] buffer = new byte[info.Length];
            new FileStream(srcFile, FileMode.Open, FileAccess.Read).Read(buffer, 0, buffer.Length);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            byte[] buffer2 = buffer;
            provider.Key = Encoding.ASCII.GetBytes("GHjiEeWw");
            provider.IV = Encoding.ASCII.GetBytes("GHjiEeWw");
            FileStream stream = new FileStream(dstFile, FileMode.Create, FileAccess.ReadWrite);
            CryptoStream stream3 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
            stream3.Write(buffer2, 0, buffer2.Length);
            stream3.Flush();
            stream3.Close();
        }

        public static string smethod(string input)
        {
            byte[] sourceArray = new MD5CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(input), 0, input.Length);
            char[] destinationArray = new char[sourceArray.Length];
            Array.Copy(sourceArray, destinationArray, sourceArray.Length);
            return new string(destinationArray);
        }

        public static string smethod1(string pToEncrypt, string sKey)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            byte[] bytes = Encoding.Default.GetBytes(pToEncrypt);
            provider.Key = Encoding.ASCII.GetBytes(sKey);
            provider.IV = Encoding.ASCII.GetBytes(sKey);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.FlushFinalBlock();
            StringBuilder builder = new StringBuilder();
            foreach (byte num2 in stream.ToArray())
            {
                builder.AppendFormat("{0:X2}", num2);
            }
            builder.ToString();
            return builder.ToString();
        }

        public static string smethod2(string pToDecrypt, string sKey)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            byte[] buffer = new byte[pToDecrypt.Length / 2];
            for (int i = 0; i < (pToDecrypt.Length / 2); i++)
            {
                int num2 = Convert.ToInt32(pToDecrypt.Substring(i * 2, 2), 0x10);
                buffer[i] = (byte) num2;
            }
            provider.Key = Encoding.ASCII.GetBytes(sKey);
            provider.IV = Encoding.ASCII.GetBytes(sKey);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            new StringBuilder();
            return Encoding.Default.GetString(stream.ToArray());
        }

        public enum EncryptionType
        {
            RSA,
            DES
        }
    }
}

